﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using DLLPC.Utilities;
using Advantech.Motion;
using Advantech.MotionComponent;
using System.Security.Permissions;
using RRQMSocket;
using OfficeOpenXml.FormulaParsing.Excel.Functions.RefAndLookup;

namespace VisionCore
{
    public class MotionHelper : DLLPC.MotionHelper
    {
        private IntPtr m_DeviceHandle = IntPtr.Zero;
        private uint DiCount_0 = 0;
        private uint DoCount_0 = 0;
        private uint DiCount_1 = 0;
        private uint DoCount_1 = 0;
        private uint AiCount_1 = 0;
        private uint AoCount_1 = 0;

        private int timecot_IO = 0;
        private int timecot_Axis = 0;
        private bool blnInit = false;
        private Thread thread;
        public MotionHelper()
        {
            #region OLD
            ExcST_Y = new AxisHelper(new MotionDevice(), 99);
            ExcST_R = new AxisHelper(new MotionDevice(), 99);
            ExcTR_X = new AxisHelper(new MotionDevice(), 99);
            ExcTR_Z = new AxisHelper(new MotionDevice(), 99);
            ExcCCD = new AxisHelper(new MotionDevice(), 99);
            Efem_X = new AxisHelper(new MotionDevice(), 99);
            ExcTR_R = new AxisHelper(new MotionDevice(), 99);
            SortST_X = new AxisHelper(new MotionDevice(), 99);
            SortST_Y = new AxisHelper(new MotionDevice(), 99);
            LDTR_R = new AxisHelper(new MotionDevice(), 99);
            ULDTR_R = new AxisHelper(new MotionDevice(), 99);
            LDTR_1_Z = new AxisHelper(new MotionDevice(), 99);
            LDTR_1_R = new AxisHelper(new MotionDevice(), 99);
            LDTR_2_Z = new AxisHelper(new MotionDevice(), 99);
            LDTR_2_R = new AxisHelper(new MotionDevice(), 99);
            ULDTR_1_Z = new AxisHelper(new MotionDevice(), 99);
            ULDTR_1_R = new AxisHelper(new MotionDevice(), 99);
            ULDTR_2_Z = new AxisHelper(new MotionDevice(), 99);
            ULDTR_2_R = new AxisHelper(new MotionDevice(), 99);
            LaserST_R = new AxisHelper(new MotionDevice(), 99);
            ULDST_X = new AxisHelper(new MotionDevice(), 99);
            ULDST_Y = new AxisHelper(new MotionDevice(), 99);
            ULDST_Ext = new AxisHelper(new MotionDevice(), 99);
            Efem_X = new AxisHelper(new MotionDevice(), 99);
            ULDPort_Z_L = new AxisHelper(new MotionDevice(), 99);
            ULDPort_Z_R = new AxisHelper(new MotionDevice(), 99);
            #endregion

            CV1_TrackSW_Y = new AxisHelper(new MotionDevice(), 99);
            CV2_TrackSW_Y = new AxisHelper(new MotionDevice(), 99);
            CCD1_X = new AxisHelper(new MotionDevice(), 99);
            CCD1_Y = new AxisHelper(new MotionDevice(), 99);
            CCD1_Z = new AxisHelper(new MotionDevice(), 99);
            CCD2_X = new AxisHelper(new MotionDevice(), 99);
            CCD2_Y = new AxisHelper(new MotionDevice(), 99);
            CCD2_Z = new AxisHelper(new MotionDevice(), 99);
            CV1_Driver = new AxisHelper(new MotionDevice(), 99);
            CV2_Driver = new AxisHelper(new MotionDevice(), 99);
            Insp1_CV1 = new AxisHelper(new MotionDevice(), 99);
            Insp1_CV2 = new AxisHelper(new MotionDevice(), 99);
            Insp2_CV1 = new AxisHelper(new MotionDevice(), 99);
            Insp2_CV2 = new AxisHelper(new MotionDevice(), 99);
            Insp3_CV1 = new AxisHelper(new MotionDevice(), 99);
            Insp3_CV2 = new AxisHelper(new MotionDevice(), 99);
            Insp4_CV1 = new AxisHelper(new MotionDevice(), 99);
            Insp4_CV2 = new AxisHelper(new MotionDevice(), 99);
            CV1_Y = new AxisHelper(new MotionDevice(), 99);
            CV2_Y = new AxisHelper(new MotionDevice(), 99);
            Insp1_Y = new AxisHelper(new MotionDevice(), 99);
            Insp2_Y = new AxisHelper(new MotionDevice(), 99);
            Insp3_Y = new AxisHelper(new MotionDevice(), 99);
            Insp4_Y = new AxisHelper(new MotionDevice(), 99);
            //Insp1_RM_Z = new AxisHelper_SMC(this, axis1);
            //Insp2_RM_Z = new AxisHelper(new MotionDevice(), 99);
            //Insp3_RM_Z = new AxisHelper(new MotionDevice(), 99);
            //Insp4_RM_Z = new AxisHelper_SMC(this, axis1);

            //Insp4_RM = new AxisHelper(new MotionDevice(),99);
            

        }

        public DI_X DI { get; set; } = new DI_X(96);
        public DO_Y DO { get; set; } = new DO_Y(64);

        public Ao_All Ao { get; set; } = new Ao_All(4);

        #region 轴申明
        public AxisHelper ExcST_Y { get; set; }
        public AxisHelper ExcST_R { get; set; }
        public AxisHelper ExcCCD { get; set; }
        public AxisHelper ExcTR_X { get; set; }
        public AxisHelper ExcTR_Z { get; set; }
        public AxisHelper ExcTR_R { get; set; }
        public AxisHelper SortST_X { get; set; }
        public AxisHelper SortST_Y { get; set; }
        public AxisHelper LDTR_R { get; set; }
        public AxisHelper ULDTR_R { get; set; }
        public AxisHelper LDTR_1_Z { get; set; }
        public AxisHelper LDTR_1_R { get; set; }
        public AxisHelper_SMC LDTR_1_Clamp { get; set; }
        public AxisHelper LDTR_2_Z { get; set; }
        public AxisHelper LDTR_2_R { get; set; }
        public AxisHelper_SMC LDTR_2_Clamp { get; set; }
        public AxisHelper ULDTR_1_Z { get; set; }
        public AxisHelper ULDTR_1_R { get; set; }
        public AxisHelper_SMC ULDTR_1_Clamp { get; set; }
        public AxisHelper ULDTR_2_Z { get; set; }
        public AxisHelper ULDTR_2_R { get; set; }
        public AxisHelper_SMC ULDTR_2_Clamp { get; set; }
        public AxisHelper LaserST_R { get; set; }
        public AxisHelper ULDST_X { get; set; }
        public AxisHelper ULDST_Y { get; set; }
        public AxisHelper ULDST_Ext { get; set; }
        public AxisHelper Efem_X { get; set; }
        public AxisHelper ULDPort_Z_L { get; set; }
        public AxisHelper ULDPort_Z_R { get; set; }
        public AxisHelper CCD_1 { get; set; }
        public AxisHelper CCD_2 { get; set; }
        public AxisHelper CCD_3 { get; set; }
        public AxisHelper CCD_4 { get; set; }
        public AxisHelper CCD_5 { get; set; }
        public AxisHelper CCD_6 { get; set; }
        public AxisHelper SortST_R { get; set; }
        public AxisHelper Buf_1 { get; set; }
        public AxisHelper Buf_2 { get; set; }
        public AxisHelper Buf_3 { get; set; }

        private SMCInfo axis1 = new SMCInfo();
        private SMCInfo axis2 = new SMCInfo();
        private SMCInfo axis3 = new SMCInfo();
        private SMCInfo axis4 = new SMCInfo();
       
        #endregion
        //三菱伺服
        public AxisHelper CV1_TrackSW_Y { get; set; }
        public AxisHelper CV2_TrackSW_Y { get; set; }
        public AxisHelper CCD1_X { get; set; }
        public AxisHelper CCD1_Y { get; set; }
        public AxisHelper CCD1_Z { get; set; }
        public AxisHelper CCD2_X { get; set; }
        public AxisHelper CCD2_Y { get; set; }
        public AxisHelper CCD2_Z { get; set; }

        //鸣志驱动器
        public AxisHelper CV1_Driver { get; set; }
        public AxisHelper CV2_Driver { get; set; }
        public AxisHelper Insp1_CV1 { get; set; }
        public AxisHelper Insp1_CV2 { get; set; }
        public AxisHelper Insp2_CV1 { get; set; }
        public AxisHelper Insp2_CV2 { get; set; }
        public AxisHelper Insp3_CV1 { get; set; }
        public AxisHelper Insp3_CV2 { get; set; }
        public AxisHelper Insp4_CV1 { get; set; }
        public AxisHelper Insp4_CV2 { get; set; }
        public AxisHelper CV1_Y { get; set; }
        public AxisHelper CV2_Y { get; set; }
        public AxisHelper Insp1_Y { get; set; }
        public AxisHelper Insp2_Y { get; set; }
        public AxisHelper Insp3_Y { get; set; }
        public AxisHelper Insp4_Y { get; set; }

        public AxisHelper Insp4_RM { get; set; }




        //增广力控
        public RM_Helper Rm_Insp1_Z { get; set; }
        public RM_Helper Rm_Insp2_Z { get; set; }
        public RM_Helper Rm_Insp3_Z { get; set; }
        public RM_Helper Rm_Insp4_Z { get; set; }

        public bool IsRun { get; set; } = false;

        public void Start()
        {
            DIUpdateEventHandler += new Action<byte[]>(DI.Update);
            DOUpdateEventHandler += new Func<byte[], byte[]>(DO.Update);
            //AoUpdateEventHandler += new Func<float[], float[]>(Ao.Update);

            thread = new Thread(ThreadFuction);
            thread.IsBackground = true;
            thread.Priority = ThreadPriority.Normal;
            thread.Start();
            blnInit = true;
        }

        bool firstScanFlag = true;
        bool[] devOpened;
        private void ThreadFuction()
        {
            ProcessHelper.SetThreadAffinityMask(ProcessHelper.GetCurrentThread(), new UIntPtr(ProcessHelper.SetCpuID(3)));
            int intStatus = 0;
            while (blnInit)
            {
                switch (intStatus)
                {
                    case 0:
                        if (GetDevNum())
                        {
                            devOpened = new bool[Devices.Length];
                            Slave0_Counts[0] = 20;
                            Slave1_Counts[0] = 26;                           
                            intStatus = 1;
                        }
                        break;
                    case 1:
                        if (!devOpened[0])
                        {
                            devOpened[0] = DevOpen(0);
                        }
                        if (devOpened[0])
                        {
                            intStatus = 2;
                        }
                        else
                        {
                            Thread.Sleep(1000);
                        }
                        break;
                    case 2:                        
                        GetU32Property(0,out DiCount_0, out DoCount_0);
                        IOStartPort = 0;
                        SetDevEvt(0);
                        //SetDevEvt(1);
                        if (AxInit())
                        {
                            intStatus = 3;
                        }
                        break;
                    case 3:
                        if (AxOpen())
                        {
                            intStatus = 4;
                        }
                        break;
                    case 4:
                        LoadCfg(0);
                        //LoadCfg(1);
                        SetParam();
                        SVON();
                        intStatus = 5;
                        break;
                    case 5:
                        Polling();
                        if (ReOpenEnable && !IsRun /*&& false*/)
                        {
                            Task.Run(() =>
                            {
                                DevRingDisCnt = 0;
                                while (blnInit)
                                {
                                    if (CheckDevEvt(0)|| CheckDevEvt(1))
                                    {
                                        intStatus = 1;
                                        IsRun = false;
                                    }

                                    SystemTimeCounter.timeBeginPeriod(1);
                                    Thread.Sleep(1);
                                    SystemTimeCounter.timeEndPeriod(1);
                                }
                            });
                        }

                        if (firstScanFlag || !IsRun)
                        {
                            Thread.Sleep(100);
                            IsRun = true;
                            firstScanFlag = false;
                        }

                        break;
                }

                SystemTimeCounter.timeBeginPeriod(1);
                Thread.Sleep(1);
                SystemTimeCounter.timeEndPeriod(1);
            }
            AxClose();
            if (Devices!=null)
            {
                for (int i = 0; i < Devices.Length; i++)
                {
                    Devices[i].Close();
                }
            }           
        }

        public void Quit()
        {
            blnInit = false;
            IsRun = false;
        }

        public override bool AxOpen()
        {
            Devices[0].MultiCmpAxisEnable = 0;
            CV1_TrackSW_Y.OpenAxis();
            CV2_TrackSW_Y.OpenAxis();
            CCD1_X.OpenAxis();
            CCD1_Y.OpenAxis();
            CCD1_Z.OpenAxis();
            CCD2_X.OpenAxis();
            CCD2_Y.OpenAxis();
            CCD2_Z.OpenAxis();
            CV1_Driver.OpenAxis();
            CV2_Driver.OpenAxis();
            Insp1_CV1.OpenAxis();
            Insp1_CV2.OpenAxis();
            Insp2_CV1.OpenAxis();
            Insp2_CV2.OpenAxis();
            Insp3_CV1.OpenAxis();
            Insp3_CV2.OpenAxis();
            Insp4_CV1.OpenAxis();
            Insp4_CV2.OpenAxis();
            CV1_Y.OpenAxis();
            CV2_Y.OpenAxis();
            Insp1_Y.OpenAxis();
            Insp2_Y.OpenAxis();
            Insp3_Y.OpenAxis();
            Insp4_Y.OpenAxis();        
            

            LogMsg(1, "motion initial done");

            return true;
        }

        public bool AxInitOk = false;
        private bool AxInit()
        {
            Devices[0].EmgLogic = (uint)EmgLogic.EMG_ACT_HIGH;

            CV1_TrackSW_Y = new AxisHelper(Devices[0], 1, 1000);
            CV2_TrackSW_Y = new AxisHelper(Devices[0], 2, 1000);
            CCD1_X = new AxisHelper(Devices[0], 3, 1000);
            CCD1_Y = new AxisHelper(Devices[0], 4, 1000);
            CCD1_Z = new AxisHelper(Devices[0], 5, 1000);
            CCD2_X = new AxisHelper(Devices[0], 6, 1000);
            CCD2_Y = new AxisHelper(Devices[0], 7, 1000);
            CCD2_Z = new AxisHelper(Devices[0], 8, 1000);
            CV1_Y = new AxisHelper(Devices[0], 0, 512);
            CV2_Y = new AxisHelper(Devices[0], 9, 512);
            Insp1_Y = new AxisHelper(Devices[0], 10, 819.2);
            Insp2_Y = new AxisHelper(Devices[0], 11, 819.2);
            Insp3_Y = new AxisHelper(Devices[0], 12, 819.2);
            Insp4_Y = new AxisHelper(Devices[0], 13, 819.2);

            Insp4_RM = new AxisHelper(Devices[0], 13, 819.2);

            CV1_Driver = new AxisHelper(Devices[0], 14, 1000);
            CV2_Driver = new AxisHelper(Devices[0], 15, 1000);
            Insp1_CV1 = new AxisHelper(Devices[0], 16, 1000);
            Insp1_CV2 = new AxisHelper(Devices[0], 17, 1000);
            Insp2_CV1 = new AxisHelper(Devices[0], 18, 1000);
            Insp2_CV2 = new AxisHelper(Devices[0], 19, 1000);
            Insp3_CV1 = new AxisHelper(Devices[0], 20, 1000);
            Insp3_CV2 = new AxisHelper(Devices[0], 21, 1000);
            Insp4_CV1 = new AxisHelper(Devices[0], 22, 1000);
            Insp4_CV2 = new AxisHelper(Devices[0], 23, 1000);
            
            Rm_Insp1_Z = new RM_Helper(this, 0x10);
            Rm_Insp2_Z = new RM_Helper(this, 0x11);
            Rm_Insp3_Z = new RM_Helper(this, 0x12);
            Rm_Insp4_Z = new RM_Helper(this, 0x13);

            LogMsg(0, "Axis initial done");
            AxInitOk = true;
            return true;
        }

        public bool WriteSDOData(ushort ringNo, ushort slaveIP, ushort Index, ushort SubIndex, int value)
        {
            if (Devices[0].Handle != IntPtr.Zero && Motion.mAcm_DevWriteSDOData(Devices[0].Handle, ringNo, slaveIP, Index, SubIndex, value) == 0)
            {
                return true;
            }
            return false;
        }
        public bool WriteSDOData(ushort ringNo, ushort slaveIP, ushort Index, ushort SubIndex, byte value)
        {
            if (Devices[0].Handle != IntPtr.Zero && Motion.mAcm_DevWriteSDOData(Devices[0].Handle, ringNo, slaveIP, Index, SubIndex, value) == 0)
            {
                return true;
            }
            return false;
        }

        public void ReadSDOData(ushort ringNo, ushort slaveIP, ushort Index, ushort SubIndex, ref float value)
        {          
            Motion.mAcm_DevReadSDOData(Devices[0].Handle, ringNo, slaveIP, Index, SubIndex, ref value);           
        }

        public void ReadSDOData(ushort ringNo, ushort slaveIP, ushort Index, ushort SubIndex, ref byte value)
        {
            if (Devices[0].Handle != IntPtr.Zero)
            {
                uint ret = Motion.mAcm_DevReadSDOData(Devices[0].Handle, ringNo, slaveIP, Index, SubIndex, ref value);

            }                  
        }

        private bool SetParam()
        {
            CV1_TrackSW_Y.axis.Jerk = 1;
            CV2_TrackSW_Y.axis.Jerk = 1;
            CCD1_X.axis.Jerk = 1;
            CCD1_Y.axis.Jerk = 1;
            CCD1_Z.axis.Jerk = 1;
            CCD2_X.axis.Jerk = 1;
            CCD2_Y.axis.Jerk = 1;
            CCD2_Z.axis.Jerk = 1;
            CV1_Driver.axis.Jerk = 1;
            CV2_Driver.axis.Jerk = 1;
            Insp1_CV1.axis.Jerk = 1;
            Insp1_CV2.axis.Jerk = 1;
            Insp2_CV1.axis.Jerk = 1;
            Insp2_CV2.axis.Jerk = 1;
            Insp3_CV1.axis.Jerk = 1;
            Insp3_CV2.axis.Jerk = 1;
            Insp4_CV1.axis.Jerk = 1;
            Insp4_CV2.axis.Jerk = 1;
            CV1_Y.axis.Jerk = 1;
            CV2_Y.axis.Jerk = 1;
            Insp1_Y.axis.Jerk = 1;
            Insp2_Y.axis.Jerk = 1;
            Insp3_Y.axis.Jerk = 1;
            Insp4_Y.axis.Jerk = 1;

            CV1_TrackSW_Y.VelHighest = 1000;
            CV2_TrackSW_Y.VelHighest = 1000;
            CCD1_X.VelHighest = 1000;
            CCD1_Y.VelHighest = 1000;
            CCD1_Z.VelHighest = 500;
            CCD2_X.VelHighest = 1000;
            CCD2_Y.VelHighest = 1000;
            CCD2_Z.VelHighest = 500;
            CV1_Driver.VelHighest = 1000;
            CV2_Driver.VelHighest = 1000;
            Insp1_CV1.VelHighest = 1000;
            Insp1_CV2.VelHighest = 1000;
            Insp2_CV1.VelHighest = 1000;
            Insp2_CV2.VelHighest = 1000;
            Insp3_CV1.VelHighest = 1000;
            Insp3_CV2.VelHighest = 1000;
            Insp4_CV1.VelHighest = 1000;
            Insp4_CV2.VelHighest = 1000;
            CV1_Y.VelHighest = 500;
            CV2_Y.VelHighest = 500;
            Insp1_Y.VelHighest = 500;
            Insp2_Y.VelHighest = 500;
            Insp3_Y.VelHighest = 500;
            Insp4_Y.VelHighest = 500;
           

            LogMsg(2, "Axis param initial done");
            return true;
        }

        public void SVON()
        {
            CV1_TrackSW_Y.SetSvOn(1);
            CV2_TrackSW_Y.SetSvOn(1);
            CCD1_X.SetSvOn(1);
            CCD1_Y.SetSvOn(1);
            CCD1_Z.SetSvOn(1);
            CCD2_X.SetSvOn(1);
            CCD2_Y.SetSvOn(1);
            CCD2_Z.SetSvOn(1);
            CV1_Driver.SetSvOn(1);
            CV2_Driver.SetSvOn(1);
            Insp1_CV1.SetSvOn(1);
            Insp1_CV2.SetSvOn(1);
            Insp2_CV1.SetSvOn(1);
            Insp2_CV2.SetSvOn(1);
            Insp3_CV1.SetSvOn(1);
            Insp3_CV2.SetSvOn(1);
            Insp4_CV1.SetSvOn(1);
            Insp4_CV2.SetSvOn(1);
            CV1_Y.SetSvOn(1);
            CV2_Y.SetSvOn(1);
            Insp1_Y.SetSvOn(1);
            Insp2_Y.SetSvOn(1);
            Insp3_Y.SetSvOn(1);
            Insp4_Y.SetSvOn(1);
        }


        public void ResetCounters(AxisHelper axis)
        {
            uint ret = Motion.mAcm_AxSetActualPosition(axis.axis.Handle,0);
            uint ret1 = Motion.mAcm_AxSetCmdPosition(axis.axis.Handle, 0);
        }
        private bool Polling()
        {
            timecot_IO++;
            timecot_Axis++;

            if (blnInit)
            {
                if (timecot_Axis >= 10)
                {
                    timecot_Axis = 0;
                    CV1_TrackSW_Y.GetMotionIO();
                    CV2_TrackSW_Y.GetMotionIO();
                    CCD1_X.GetMotionIO();
                    CCD1_Y.GetMotionIO();
                    CCD1_Z.GetMotionIO();
                    CCD2_X.GetMotionIO();
                    CCD2_Y.GetMotionIO();
                    CCD2_Z.GetMotionIO();
                    CV1_Driver.GetMotionIO();
                    CV2_Driver.GetMotionIO();
                    Insp1_CV1.GetMotionIO();
                    Insp1_CV2.GetMotionIO();
                    Insp2_CV1.GetMotionIO();
                    Insp2_CV2.GetMotionIO();
                    Insp3_CV1.GetMotionIO();
                    Insp3_CV2.GetMotionIO();
                    Insp4_CV1.GetMotionIO();
                    Insp4_CV2.GetMotionIO();
                    CV1_Y.GetMotionIO();
                    CV2_Y.GetMotionIO();
                    Insp1_Y.GetMotionIO();
                    Insp2_Y.GetMotionIO();
                    Insp3_Y.GetMotionIO();
                    Insp4_Y.GetMotionIO();
                    

                    CV1_TrackSW_Y.GetMotionStatus();
                    CV2_TrackSW_Y.GetMotionStatus();
                    CCD1_X.GetMotionStatus();
                    CCD1_Y.GetMotionStatus();
                    CCD1_Z.GetMotionStatus();
                    CCD2_X.GetMotionStatus();
                    CCD2_Y.GetMotionStatus();
                    CCD2_Z.GetMotionStatus();
                    CV1_Driver.GetMotionStatus();
                    CV2_Driver.GetMotionStatus();
                    Insp1_CV1.GetMotionStatus();
                    Insp1_CV2.GetMotionStatus();
                    Insp2_CV1.GetMotionStatus();
                    Insp2_CV2.GetMotionStatus();
                    Insp3_CV1.GetMotionStatus();
                    Insp3_CV2.GetMotionStatus();
                    Insp4_CV1.GetMotionStatus();
                    Insp4_CV2.GetMotionStatus();
                    CV1_Y.GetMotionStatus();
                    CV2_Y.GetMotionStatus();
                    Insp1_Y.GetMotionStatus();
                    Insp2_Y.GetMotionStatus();
                    Insp3_Y.GetMotionStatus();
                    Insp4_Y.GetMotionStatus();                 
                }

                //if (timecot_IO >= 100)
                //{
                //    timecot_IO = 0;
                RefreshDIValue(0, DiCount_0);
                RefreshDOValue(0, DoCount_0);
                //RefresAoValue(1, AoCount_1);
                //}
            }

            return true;
        }

        object objLock = new object();
        public void F_Home()
        {
            lock (objLock)
            {
                CV1_TrackSW_Y.HomeStart = F__home(CV1_TrackSW_Y, CV1_TrackSW_Y.HomeStart, 30, 50, 1);
                CV2_TrackSW_Y.HomeStart = F__home(CV2_TrackSW_Y, CV2_TrackSW_Y.HomeStart, 30, 50, 1);
                CCD1_X.HomeStart = F__home(CCD1_X, CCD1_X.HomeStart, 30, 30, 1);
                CCD1_Y.HomeStart = F__home(CCD1_Y, CCD1_Y.HomeStart, 30, 30, 1);
                CCD1_Z.HomeStart = F__home(CCD1_Z, CCD1_Z.HomeStart, 30, 10, 1);
                CCD2_X.HomeStart = F__home(CCD2_X, CCD2_X.HomeStart, 30, 30, 1);
                CCD2_Y.HomeStart = F__home(CCD2_Y, CCD2_Y.HomeStart, 30, 30, 1);
                CCD2_Z.HomeStart = F__home(CCD2_Z, CCD2_Z.HomeStart, 30, 10, 1);
                CV1_Y.HomeStart = F__home(CV1_Y, CV1_Y.HomeStart, 30, 30, 1);
                CV2_Y.HomeStart = F__home(CV2_Y, CV2_Y.HomeStart, 30, 30, 1);
                Insp1_Y.HomeStart = F__home(Insp1_Y, Insp1_Y.HomeStart, 30, 30, 1);
                Insp2_Y.HomeStart = F__home(Insp2_Y, Insp2_Y.HomeStart, 30, 30, 1);
                Insp3_Y.HomeStart = F__home(Insp3_Y, Insp3_Y.HomeStart, 30, 30, 1);
                Insp4_Y.HomeStart = F__home(Insp4_Y, Insp4_Y.HomeStart, 30, 30, 1);             

            }
        }

        private void LogMsg(string Msg)
        {
            string strMsg = Msg;
            LogHelper.Write(LogHelper.Module.Motion_Axis, strMsg);
        }

        private void LogMsg(int StatusCode, string Msg)
        {
            string strMsg = string.Format("StatusCode: {0}, {1}", StatusCode, Msg);
            LogHelper.Write(LogHelper.Module.Motion_Axis, strMsg);
        }
    }


}
